//
//  HOAreaSelectionViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOAreaSelectionViewController.h"
#import "HOUIMgr.h"
#import "Logger.h"
#import "HOConstants.h"


@interface HOAreaSelectionViewController ()
{
  
}
@end

@implementation HOAreaSelectionViewController
@synthesize dataSource;

- (id)initWithModule:(HO_MODULE_TYPE)type withSelType:(HO_AREA_SELECTION_TYPE)selType;
{
    if(self = [super initWithModule:type])
    {
        selectionType = selType;
    }
    return self;
        
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the
  
    

    [super initTableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(selectionType == HO_AREA_SELECTION_PROVINCE)
    {
        [self setNavigationItemTitle:@"选择省份"];
        self.dataSource = [[[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]] autorelease];
        
    }
    else if(selectionType == HO_AREA_SELECTION_CITY)
    {
        [self setNavigationItemTitle:@"选择城市"];
    }
    else if(selectionType == HO_AREA_SELECTION_AREA)
    {
        [self setNavigationItemTitle:@"选择区县"];
    }
//    LogInf(@"self.dataSource%@", self.dataSource);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    [super dealloc];
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource count];
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *AMBaseTableViewCellIdentifier = @"AMBaseTableViewCellIdentifier";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
    }
    CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    NSString *content = @"";
    id data = [self.dataSource objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    if(selectionType == HO_AREA_SELECTION_PROVINCE)
    {
        content = [data objectForKey:@"state"];
    }
    else if(selectionType == HO_AREA_SELECTION_CITY)
    {
        content = [data objectForKey:@"city"];
        if([[data objectForKey:@"areas"] count] > 0)
        {
            
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            cell.selectionStyle = UITableViewCellSelectionStyleGray;
        }
        else
        {
            
//            cell.accessoryType = UITableViewCellAccessoryNone;
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
       
        
    }
    else if(selectionType == HO_AREA_SELECTION_AREA)
    {
        content = data;
    }
    UILabel *lbl = (UILabel *)[cell viewWithTag:11];
    if(lbl == nil)
    {
        lbl = [[[UILabel alloc]initWithFrame:CGRectMake(HO_HORIZONAL_PADDING, (cellHeight - 22) / 2 , tableView.frame.size.width - 2 * HO_HORIZONAL_PADDING - 20, 22)]autorelease];
        lbl.tag = 11;
        [cell.contentView addSubview:lbl];
        
    }
    lbl.text = content;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HO_AREA_SELECTION_TYPE type;
    NSString *content = @"";
    id data = [self.dataSource objectAtIndex:indexPath.row];
    if(selectionType == HO_AREA_SELECTION_PROVINCE)
    {
        content = [data objectForKey:@"state"];
    }
    else if(selectionType == HO_AREA_SELECTION_CITY)
    {
        content = [data objectForKey:@"city"];
    }
    else if(selectionType == HO_AREA_SELECTION_AREA)
    {
        content = data;
    }
    if(selectionType == HO_AREA_SELECTION_PROVINCE)
    {
        [[HOUIMgr sharedInstance]
            sendCommand:kHO_CMD_SELECDTED_Province
                            data:content
                    toModules:@[[NSNumber  numberWithInt:moduleType]]];
        
        type = HO_AREA_SELECTION_CITY;
        HOAreaSelectionViewController *viewCtrl = [[[HOAreaSelectionViewController alloc]initWithModule:moduleType withSelType:type] autorelease];
        viewCtrl.dataSource = [data objectForKey:@"cities"];
        [self.navigationController pushViewController:viewCtrl animated:YES];
    }
    else if(selectionType == HO_AREA_SELECTION_CITY)
    {
//        id city = [data objectForKey:@"cities"];
        [[HOUIMgr sharedInstance]
         sendCommand:kHO_CMD_SELECDTED_City
         data:content
         toModules:@[[NSNumber  numberWithInt:moduleType]]];
        type = HO_AREA_SELECTION_AREA;
        if( [[data objectForKey:@"areas"] count] > 0)
        {
            HOAreaSelectionViewController *viewCtrl = [[[HOAreaSelectionViewController alloc]initWithModule:moduleType withSelType:type] autorelease];
            viewCtrl.dataSource = [data objectForKey:@"areas"];
            [self.navigationController pushViewController:viewCtrl animated:YES];
        }
        else
        {
            
            [self goBackToUserInfoUI];
        }
        
        
    }
    else if(selectionType == HO_AREA_SELECTION_AREA)
    {
        [[HOUIMgr sharedInstance]
         sendCommand:kHO_CMD_SELECDTED_AREA
         data:content
         toModules:@[[NSNumber  numberWithInt:moduleType]]];
        [self goBackToUserInfoUI];
    }
}

- (void)goBackToUserInfoUI
{
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    for(UIViewController *viewCtrl in self.navigationController.viewControllers)
    {
        if([viewCtrl isKindOfClass:[HOAreaSelectionViewController class]] && viewCtrl != self)
        {
            [[HOUIMgr sharedInstance] removeObserverFromListener:viewCtrl];
            [viewControllers removeObject:viewCtrl];
        }
    }
    self.navigationController.viewControllers = viewControllers;
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
